		TITLE	MODNAME - Copyright (C) 1994 SLR Systems

		INCLUDE	MACROS
		INCLUDE	MODULES

		PUBLIC	INSTALL_MODULE,FORCE_INSTALL_MODULE


		.DATA

		EXTERNDEF	MODULE_HASH:DWORD,MODULE_HASH_TABLE_PTR:DWORD,FIRST_MODULE_GINDEX:DWORD,LAST_MODULE_GINDEX:DWORD
		EXTERNDEF	MODULE_COUNT:DWORD

		EXTERNDEF	MODULE_GARRAY:STD_PTR_S

		EXTERNDEF	CASE_STRING_COMPARE:DWORD


		.CODE	PASS1_TEXT

INSTALL_MODULE	PROC
		;
		;EDX IS HASH,EAX IS TPTR
		;
		;RETURN EAX GINDEX, ECX PHYS
		;
		GETT	CL,NEED_MODULES_SEARCHED

		OR	CL,CL
		JZ	ALLOC_MODULE			;NOTHING IN HASH TABLE, SIMPLY ALLOCATE

		CALL	SEARCH_MODULE	

		JC	ALLOC_MODULE			;THIS MODULE NOT IN HASH TABLE, ALLOCATE NEW ONE

		ASSUME	ECX:PTR MODULE_STRUCT
		MOV	DL,[ECX]._M_FLAGS

		AND	DL,MASK M_USED
		JNZ	ALLOC_MODULE			;FOUND, BUT NAME IS DUPLICATE, ALLOCATE A NEW ONE

		RET

INSTALL_MODULE	ENDP


ALLOC_MODULE	PROC
		;
		;ALLOCATE A NEW MODULE_STRUCT
		;
		ASSUME	EAX:PTR TPTR_STRUCT

		PUSH	ESI
		LEA	ESI,[EAX]._TP_TEXT

		MOV	EAX,[EAX]._TP_LENGTH
		PUSH	EDI

		GETT	CL,NEED_MODULES_INSTALLED
		ADD	EAX,SIZEOF MODULE_STRUCT-3

		OR	CL,CL
		JNZ	L2$

		TILLMIDDLE_POOL_ALLOC		;ES:DI IS PHYS, AX LOG, WON'T NEED AGAIN TILL MIDDLE
		JMP	L3$

L2$:
		MODULE_POOL_ALLOC		;WILL NEED THROUGHOUT PASS1 AND 2
L3$:
		PUSH	EBX
		MOV	ECX,LAST_MODULE_GINDEX

		MOV	EDI,EAX
		INSTALL_POINTER_GINDEX	MODULE_GARRAY
		MOV	EDX,EDI

		MOV	EBX,EAX
		MOV	EAX,MODULE_COUNT

		TEST	ECX,ECX
		JZ	L4$

		CONVERT	ECX,ECX,MODULE_GARRAY
		ASSUME	ECX:PTR MODULE_STRUCT

		MOV	[ECX]._M_NEXT_MODULE_GINDEX,EBX
L49$:
		INC	EAX

		MOV	LAST_MODULE_GINDEX,EBX
		MOV	MODULE_COUNT,EAX

		MOV	ECX,MODULE_STRUCT._M_TEXT/4
		XOR	EAX,EAX

		REP	STOSD			;ZERO OUT RECORD

		MOV	ECX,[ESI-4]
		MOV	EAX,EBX

		SHR	ECX,2
		POP	EBX

		INC	ECX

		REP	MOVSD			;STORE TEXT

		POPM	EDI,ESI

		MOV	ECX,EDX

		RET

L4$:
		MOV	FIRST_MODULE_GINDEX,EBX
		JMP	L49$

ALLOC_MODULE	ENDP


SEARCH_MODULE	PROC	NEAR
		;
		;EAX IS TPTR
		;EDX IS HASH
		;
		PUSHM	EDI,EBX

		MOV	EDI,EAX
		ASSUME	EDI:PTR TPTR_STRUCT
		MOV	EAX,EDX

		XOR	EDX,EDX
		MOV	EBX,MODULE_HASH_TABLE_PTR

		HASHDIV	MODULE_HASH

		PUSH	ESI

		MOV	EAX,DPTR [EBX+EDX*4]
		LEA	EBX,[EBX+EDX*4-(MODULE_STRUCT._M_NEXT_HASH_GINDEX)]
MODULE_NEXT:
		TEST	EAX,EAX
		JZ	L9$

		MOV	EDX,EAX
		CONVERT	EBX,EAX,MODULE_GARRAY
		ASSUME	EBX:PTR MODULE_STRUCT
		;
		;HASH MATCHED, TRY ACTUAL TEXT COMPARE...
		;
		PUSH	EDI
		MOV	ECX,[EDI]._TP_LENGTH

		LEA	EDI,[EDI]._TP_TEXT
		LEA	ESI,[EBX]._M_TEXT

		CALL	CASE_STRING_COMPARE

		POP	EDI
		MOV	EAX,[EBX]._M_NEXT_HASH_GINDEX

		JNZ	MODULE_NEXT

		MOV	ECX,EBX
		POP	ESI

		POPM	EBX,EDI

		MOV	EAX,EDX

		RET

L9$:
		MOV	ECX,EBX
		MOV	EAX,EDI

		CMP	ESP,-1
		POP	ESI

		POPM	EBX,EDI

		RET

SEARCH_MODULE	ENDP


FORCE_INSTALL_MODULE	PROC
		;
		;EAX IS TPTR
		;EDX IS HASH
		;
		;RETURNS EAX IS GINDEX
		;	ECX IS ADDRESS
		;
		MOV	CL,-1

		SETT	NEED_MODULES_SEARCHED,CL
		CALL	SEARCH_MODULE

		JNC	L9$				;ALREADY THERE, RETURN IT

		MOV	DL,-1
		PUSH	ECX

		SETT	NEED_MODULES_INSTALLED,DL
		CALL	ALLOC_MODULE

		POP	EDX

		RESS	NEED_MODULES_INSTALLED

		ASSUME	EDX:PTR MODULE_STRUCT
		MOV	[EDX]._M_NEXT_HASH_GINDEX,EAX
L9$:
		RET

FORCE_INSTALL_MODULE	ENDP


		END

